package com.zxn.mvvm.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.zxn.mvvm.R
import java.lang.reflect.Field

/**
 *  Updated by zxn on 2021.05.29.
 */
abstract class BaseDialogFragment : AppCompatDialogFragment(), IView, IToastView {

    companion object {
        private const val TAG = "BaseDialogFragment"
    }

    override lateinit var mContext: androidx.appcompat.app.AppCompatActivity

    override val layoutResId: Int = 0

    override val layoutRoot: View? by lazy {
        onCreateRootView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, initTheme())
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        (activity as AppCompatActivity).also { mContext = it }

        createObserver()

        onInitView()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = if (layoutRoot != null) layoutRoot else (if (layoutResId <= 0) super.onCreateView(
        inflater,
        container,
        savedInstanceState
    )
    else inflater.inflate(layoutResId, container, false))

    override fun onCreateRootView(): View? = null

    override fun createObserver() {

    }

    /**
     * 重写此方法可以更换主题
     *
     * @return
     */
    open fun initTheme(): Int {
        //return R.style.BaseDialog_FullScreen
        return R.style.BaseDialog_Nice
    }

    fun show(manager: FragmentManager) {
        super.show(manager, TAG)
    }

    open fun showAllowingStateLoss(manager: FragmentManager) {
        /*mDismissed = false
        mShownByMe = true
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commit()*/
        try {
            val dismissed: Field = DialogFragment::class.java.getDeclaredField("mDismissed")
            dismissed.isAccessible = true
            dismissed.set(this, false)
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
        try {
            val shown: Field = DialogFragment::class.java.getDeclaredField("mShownByMe")
            shown.isAccessible = true
            shown.set(this, true)
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
        val ft = manager.beginTransaction()
        ft.add(this, TAG)
        ft.commitAllowingStateLoss()
    }

    fun showNow(manager: FragmentManager) {
        super.showNow(manager, TAG)
    }

    fun show(transaction: FragmentTransaction): Int {
        return super.show(transaction, TAG)
    }

    override fun showToast(msg: Int) {
        if (activity is BaseActivity) {
            val activity = activity as BaseActivity
            activity.showToast(msg)
        }
    }

    override fun showToast(msg: String) {
        if (activity is BaseActivity) {
            val activity = activity as BaseActivity
            activity.showToast(msg)
        }
    }

}